package com.kgdev.cweb.servlets;

import javax.faces.bean.ManagedBean;
import javax.servlet.http.HttpServlet;

@ManagedBean(name="imageServlet")
public class ImageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	/*
	@EJB
	
	
	private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Get ID from request.
		String imageId = request.getParameter("id");

		// Check if ID is supplied to the request.
		if (imageId == null) {
			// Do your thing if the ID is not supplied to the request.
			// Throw an exception, or send 404, or show default/warning image,
			// or just ignore it.
			response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
			return;
		}

		// Lookup Image by ImageId in database.
		// Do your "SELECT * FROM Image WHERE ImageID" thing.
		Image image = imageDAO.find(imageId);

		// Check if image is actually retrieved from database.
		if (image == null) {
			// Do your thing if the image does not exist in database.
			// Throw an exception, or send 404, or show default/warning image,
			// or just ignore it.
			response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
			return;
		}

		// Init servlet response.
		response.reset();
		response.setBufferSize(DEFAULT_BUFFER_SIZE);
		response.setContentType(image.getContentType());
		response.setHeader("Content-Length", String.valueOf(image.getLength()));
		response.setHeader("Content-Disposition",
				"inline; filename=\"" + image.getName() + "\"");

		// Prepare streams.
		BufferedInputStream input = null;
		BufferedOutputStream output = null;

		try {
			// Open streams.
			input = new BufferedInputStream(image.getContent(),
					DEFAULT_BUFFER_SIZE);
			output = new BufferedOutputStream(response.getOutputStream(),
					DEFAULT_BUFFER_SIZE);

			// Write file contents to response.
			byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
			int length;
			while ((length = input.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
		} finally {
			// Gently close streams.
			close(output);
			close(input);
		}
	}


	private static void close(Closeable resource) {
		if (resource != null) {
			try {
				resource.close();
			} catch (IOException e) {
				// Do your thing with the exception. Print it, log it or mail
				// it.
				e.printStackTrace();
			}
		}
	}
*/
}